#include "StdAfx.h"
#include "PlaylistBox.h"

CPlaylistBox::CPlaylistBox(void)
{
}


CPlaylistBox::~CPlaylistBox(void)
{
}
BEGIN_MESSAGE_MAP(CPlaylistBox, CListBox)
	ON_WM_CREATE()
	ON_WM_DROPFILES()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()


int CPlaylistBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CListBox::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	this->DragAcceptFiles(true);
	return 0;
}


void CPlaylistBox::OnDropFiles(HDROP hDropInfo)
{
	// TODO: Add your message handler code here and/or call default
	int nFiles = DragQueryFile(hDropInfo, (UINT)-1, NULL, 0);
	for (int i=0; i<nFiles; i++)
	{
		TCHAR* pFileName=new TCHAR[_MAX_PATH];
		DragQueryFile(hDropInfo, i, pFileName, _MAX_PATH);
		AddFile(pFileName);
	}
	DragFinish(hDropInfo);

	CListBox::OnDropFiles(hDropInfo);
}


void CPlaylistBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	LPCTSTR m_ItemText = (LPCTSTR) lpDrawItemStruct->itemData;

	if(m_ItemText != NULL)
	{
		CDC dc;

		dc.Attach(lpDrawItemStruct->hDC);

		CSize m_TextLen=dc.GetTextExtent(m_ItemText, (int)_tcslen(m_ItemText));
		if(GetHorizontalExtent()<m_TextLen.cx)
			SetHorizontalExtent(m_TextLen.cx);

		COLORREF crOldTextColor = dc.GetTextColor();
		COLORREF crOldBkColor = dc.GetBkColor();

		if ((lpDrawItemStruct->itemAction | ODA_SELECT) && (lpDrawItemStruct->itemState & ODS_SELECTED))
		{
			dc.SetBkColor(RGB(205,201,201));
			dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(205,201,201));
		}
		else
		{
			dc.FillSolidRect(&lpDrawItemStruct->rcItem, crOldBkColor);
		}
		if(lpDrawItemStruct->itemID==m_PlayListFunc.m_CurFileNum-1)
		{
			dc.SetTextColor(RGB(148,0,211));
		}
		else
		{
			dc.SetTextColor(crOldTextColor);
		}


		dc.DrawText(m_ItemText, (int)_tcslen(m_ItemText), &lpDrawItemStruct->rcItem, DT_LEFT|DT_SINGLELINE|DT_VCENTER);

		dc.SetTextColor(crOldTextColor);
		dc.SetBkColor(crOldBkColor);
		dc.Detach();
	}
}


int CPlaylistBox::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct)
{

	// TODO:  Add your code to determine the sorting order of the specified items
	// return -1 = item 1 sorts before item 2
	// return 0 = item 1 and item 2 sort the same
	// return 1 = item 1 sorts after item 2

	return 0;
}


void CPlaylistBox::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	CListBox::OnLButtonDown(nFlags, point);
}

void CPlaylistBox::AddFile(CString FileName)
{
	m_PlayListFunc.AddToPlaylist(FileName);
	static CString *ShortFileName;
	ShortFileName=new CString(FileName);
	int SymbNum=ShortFileName->GetLength()-ShortFileName->ReverseFind('\\')-1;
	*ShortFileName=ShortFileName->Right(SymbNum);
	this->AddString(*ShortFileName);
}

void CPlaylistBox::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	BOOL m_IsOutside;
	int m_Item=ItemFromPoint(point, m_IsOutside);
	if(!m_IsOutside)
	{
		CString m_FileName=m_PlayListFunc.SetPlayFile(GetCurSel()+1);
		RedrawWindow();
		BSTR m_NextFile=SysAllocString(m_FileName);
		m_ActiveXPlayer->FilePlay(&m_NextFile);
	}


	CListBox::OnLButtonDblClk(nFlags, point);
}
